package com.es.controller;

import com.es.service.ItemsEsService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author: 顾志杰
 * Date: 2019/11/13
 * Time: 9:33
 */
@RestController
@RequestMapping("items")
public class ItemsController {

    private final ItemsEsService itemsEsService;


    @Autowired
    public ItemsController(ItemsEsService itemsEsService) {
        this.itemsEsService = itemsEsService;
    }

    @GetMapping("/hello")
    public Object hello() {
        return "Hello ElasticSearch";
    }

    @GetMapping("/search")
    public IMOOCJSONResult search(String keywords,
                                  String sort,
                                  Integer page,
                                  Integer pageSize) {
        if (StringUtils.isBlank(keywords)) {
            return IMOOCJSONResult.errorMsg(null);
        }
        if(page==null) {
            page=1;
        }
        if(pageSize==null) {
            pageSize=20;
        }
        page --;
        PagedGridResult comments = itemsEsService.seathItems(keywords, sort, page, pageSize);
        return IMOOCJSONResult.ok(comments);
    }

}
